//
//  DBNewFeatureController.swift
//  大波微博swift
//
//  Created by 大波 on 16/2/29.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

private let itemCount = 4

class DBNewFeatureController: UICollectionViewController {

    var flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView!.registerClass(DBNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        setupCollectionViewLayout()
    }
    
    init() {

        super.init(collectionViewLayout: flowLayout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///设置collectionView布局
    func setupCollectionViewLayout() {
        
        // 设置布局
        flowLayout.itemSize = UIScreen.mainScreen().bounds.size
        
        // 设置滚动方向
        //UICollectionViewScrollDirection.Horizontal:水平
        //UICollectionViewScrollDirection.Vertical:垂直
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        // 设置item之间的水平间隙
        //minimumInteritemSpacing:垂直
        //minimumLineSpacing:水平
        flowLayout.minimumLineSpacing = 0
        
        // 设置分页
        collectionView?.pagingEnabled = true
        
        // 去除弹簧效果
        collectionView?.bounces = false
        
        // 隐藏水平滚动条
        collectionView?.showsHorizontalScrollIndicator = false
    }
}

// MARK: UICollectionViewDataSource

extension DBNewFeatureController {
    
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return 1
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return itemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! DBNewFeatureCell
        
        cell.itemIndex = indexPath.item
        
        return cell
    }
}

//MARK: - UICollectionViewDelegate

extension DBNewFeatureController {
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        //获取当前cell的index
        let index = (Int)(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        
        if index == 3 {
            
            let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! DBNewFeatureCell
            
            cell.startAnimation()
        }
    }
}

///******************* cell *******************///

//MARK: - DBNewFeatureCell

class DBNewFeatureCell: UICollectionViewCell {
    
    //MARK: - DBNewFeatureCell 属性
    
    var itemIndex: Int = 0 {
        
        didSet {
            
            cellBackgroundView.image = UIImage(named: "new_feature_" + "\(itemIndex + 1)")
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - DBNewFeatureCell 准备UI
    
    func prepareUI() {
        
        addSubview(cellBackgroundView)
        
        addSubview(beginButton)
        
        cellBackgroundView.translatesAutoresizingMaskIntoConstraints = false
        
        beginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //背景
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bgView" : cellBackgroundView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bgView" : cellBackgroundView]))
        
        //按钮
        addConstraint(NSLayoutConstraint(item: beginButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: cellBackgroundView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -100))
        
        addConstraint(NSLayoutConstraint(item: beginButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: cellBackgroundView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    }

    ///按钮点击事件
    func beginBtnDidClick(button: UIButton) {
        
//        print(__FUNCTION__)
        AppDelegate.outSwitchMain(DBUserAccountViewModel.shareUserAccountViewModel.isUserLogin)
    }
    
    ///开始动画
    func startAnimation() {
        
        beginButton.hidden = false
        
        beginButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(0.4, delay: 0.8, usingSpringWithDamping: 0.3, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.beginButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
//                print("动画完成")
        }
    }
    
    //MARK: - 懒加载
    
    lazy var cellBackgroundView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    lazy var beginButton: UIButton = {
        
       let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "beginBtnDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.hidden = true
        
        return btn
    }()
}






























